package com.example.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.base.entity.Course;
import com.example.base.mapper.CourseMapper;
import com.example.base.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    CourseMapper courseMapper;

    @Override
    public IPage<Course> selectCourse(Course course, Integer page, Integer size) {

        LambdaQueryWrapper<Course> wrapper = Wrappers.lambdaQuery();
        // 课程名参数
        wrapper.like(ObjectUtils.isNotEmpty(course.getCourseName()), Course::getCourseName, course.getCourseName());
        // 课程提供方参数
        wrapper.like(ObjectUtils.isNotEmpty(course.getCourseProducer()), Course::getCourseProducer, course.getCourseProducer());
        // 删除标记参数
        wrapper.like(ObjectUtils.isNotEmpty(course.getDelSign()), Course::getDelSign, course.getDelSign());
        // 课程标签id参数
        wrapper.like(ObjectUtils.isNotEmpty(course.getSkillId()), Course::getSkillId, course.getSkillId());
        // 课程上架状态参数
        wrapper.like(ObjectUtils.isNotEmpty(course.getStatus()), Course::getStatus, course.getStatus());

        Page<Course> page1 = new Page<>(page, size);
        return courseMapper.selectPage(page1, wrapper);

    }

    @Override
    public List<Course> selectAllCourse() {
        LambdaQueryWrapper<Course> wrapper = Wrappers.lambdaQuery();
        return courseMapper.selectList(wrapper);
    }

    @Override
    public Integer addCourse(Course course)
    {
        return courseMapper.insert(course);
    }

    @Override
    public Integer updateCourse(Course course) {
        return courseMapper.updateById(course);
    }

    @Override
    public Integer delCourse(String courseId) {
        LambdaUpdateWrapper<Course> wrapper = Wrappers.lambdaUpdate();
        // 根据id更新删除标识
        wrapper.eq(Course::getCourseId, courseId).set(Course::getDelSign, 1);
        return courseMapper.update(null, wrapper);
    }

}
